using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace SpaceEventManager
{
    public class Program
    {
   
        /* Instantiate the passed in spaceManager's dictionary with maxValue number of spaces, each space
         * having names starting from space0, space1, etc; notes starting from notes0, notes1, etc;
         * dimensions of x=26, y=88, and an empty dictionary for inner spaces
         * Check for 1st space that it exists, the space at the middle point, and the last space.
         * Positive return for if they exist, negative return for if they do not exist
         */
        public static int testAddSpace(SpaceManager spaceManager, int maxValue)
        {
	        for (int i = 0 ; i < maxValue ; i++)
	        {
		        spaceManager.AddSpace(new Space("space" + i.ToString(), "notes" + i.ToString(), new Point(26, 88), new Dictionary<Space, System.Drawing.Point>()));
	        }
            if (spaceManager.DictAllSpacesNameKey.ContainsKey("space0") && spaceManager.DictAllSpacesNameKey.ContainsKey("space" + (maxValue/2).ToString()) && spaceManager.DictAllSpacesNameKey.ContainsKey("space" + (maxValue-1).ToString()))
            {
                return 1;
            }
            else
                return -1;
        }

        /* Tests space attributes of the spaces in the dictionary of spaceManager for correctness
         * iterate through the dictionary and check every space's Name, Notes, and Dimensions
         * to make sure they equal to the expected values we assigned before.
         * Returns a unique negative value for each type of incorrect behavior, and a positive value
         * for the dictionary being correct.
         */
        public static int testCheckSpace(SpaceManager spaceManager, int maxValue)
        {
            for (int i = 0 ; i < maxValue ; i++)
	        {
                try
                {
                    /* Checks space names to make sure they are correct */
                    if ((spaceManager.DictAllSpacesNameKey["space" + i.ToString()].Name.Equals("space" + i.ToString())) == false)
                    {
                        return -1;
                    }
                    /* Checks space notes to make sure they are correct */
                    if (spaceManager.DictAllSpacesNameKey["space" + i.ToString()].Notes.Equals("notes" + i.ToString()) == false)
                    {
                        return -2;
                    }
                    /* Checks space points to make sure they are correct */
                    if (spaceManager.DictAllSpacesNameKey["space" + i.ToString()].Dimensions.Equals(new Point(26, 88)) == false)
                    {
                        return -3;
                    }
                }
                catch (KeyNotFoundException)
                {
                    return -4;
                }
	        }
	        return 1;
        }

        /* Looks up the dictionary with key, adds spaceToAdd to the dictionary at key,
         * then compares the added inner space to see that it matches the actual spaces
         * returns a positive value if matches, negative value if they do not match.
         */
        public static int testAddInnerSpace(string key, SpaceManager spaceManager, Space spaceToAdd)
        {
	        spaceManager.DictAllSpacesNameKey[key].AddSpace(spaceToAdd, new Point(20,20));
            if (spaceManager.DictAllSpacesNameKey[key].InnerSpaces.ContainsKey(spaceToAdd))
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        /* Create a temporary inner space list and retrieves them by looking up the keys in
         * the space keyed by key. Check this temporary list to see that it contains the space
         * we passed in expecting it to contain. Returns positive value if it contains our space,
         * or returns negative value if it doesn't.
         */
        public static int testCheckInnerSpace(string key, SpaceManager spaceManager, Space space)
        {
            List<Space> tempInnerSpaces = new List<Space>();
            foreach(Space s in spaceManager.DictAllSpacesNameKey[key].InnerSpaces.Keys)
            {
                tempInnerSpaces.Add(s);
            }
            if (tempInnerSpaces.Contains(space))
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        /* Removes all spaces in spaceManager's dictionary,  then check to see
         * that spaces that were there before are no longer there. Returns a negative value if
         * they are there, otherwise returns a postive value
         */
        public static int testRemoveSpace(SpaceManager spaceManager)
        {
	        for (int i = 0; i < spaceManager.DictAllSpacesNameKey.Count; i++)
	        {
                spaceManager.RemoveSpace(spaceManager.DictAllSpacesNameKey["space" + i.ToString()]);
            }
            if (spaceManager.DictAllSpacesNameKey.ContainsKey("space0"))
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }

        /* Tries to updateSpace with a Space spaceToUpdate, then checks the new space's
         * attributes to make sure they match the space given some key. If they do not match the updated
         * space, then return a negative value corresponding to which attributes were not updated. If they
         * match, return positive.
         */
        public static int testUpdateSpace(string key, SpaceManager spaceManager, Space spaceToUpdate)
        {
            spaceManager.UpdateSpace(spaceToUpdate);
            if (spaceManager.DictAllSpacesNameKey[key].Notes != spaceToUpdate.Notes)
            {
                return -1;
            }
            else if (spaceManager.DictAllSpacesNameKey[key].Dimensions != spaceToUpdate.Dimensions)
            {
                return -2;
            }
            else
            {
                return 1;
            }
        }

        /* Perform a searchSpace given some spaceName, check through the returned list for the
         * spaceName that was used to search - if a string was found that does not contain the spaceName
         * used in the search, return a negative value. If a list was returned that does not have any
         * values, return negative value. Else the list was searched correctly and return positive.
         */
        public static int testSearchSpace(string spaceName, SpaceManager spaceManager)
        {
            List<string> searchedSpaces = spaceManager.SearchAllSpaces(spaceName);
            foreach (string s in searchedSpaces)
            {
                if (s.Contains(spaceName) == false)
                    return -1;
            }
            if (searchedSpaces.Count < 1)
            {
                return -1;
            }
            return 1;
        }

        static void Main(string[] args)
        {
            /* spaceManager is our Space Manager object
             * innerSpace and innerSpace2 are comparison spaces that will be used when comparing an actual
             * inner space to the space we expect to exist.
             * spaceToUpdate is the space used in UpdateSpace
             */
            SpaceManager spaceManager = new SpaceManager();
            Space innerSpace = new Space("innerSpace", "innerSpaceNotes", new Point(30, 40), new Dictionary<Space,Point>());
            Space innerSpace2 = new Space("innerSpace2", "innerSpaceNotes2", new Point(30, 40), new Dictionary<Space,Point>());
            Space falseInnerSpace = new Space("falseSpace", "falseNotes", new Point(13, 37), new Dictionary<Space,Point>());
            Space spaceToUpdate = new Space("space0", "notes new1", new Point(26, 89), new Dictionary<Space, Point>());
            Console.WriteLine("How many spaces to create for this test? ");
            int maxValue = int.Parse(Console.ReadLine());

            /* Below is a list of test criteria and test cases that were performed. */
            Console.WriteLine("\n---TEST CRITERIA---");
            Console.WriteLine("Testing AddSpace, creating " + maxValue + "spaces...");
            int AddSpace = testAddSpace(spaceManager, maxValue);

            Console.WriteLine("Checking spaces' attributes (Names, Notes, Dimensions)...");
            int CheckSpace = testCheckSpace(spaceManager, maxValue);

            Console.WriteLine("Testing AddInnerSpace, adding innerSpace to space with key 'space0'...");
            int AddInnerSpace = testAddInnerSpace("space0", spaceManager, innerSpace);

            Console.WriteLine("Testing AddInnerSpace, adding innerSpace2 to space with key 'space0'...");
            int AddInnerSpace2 = testAddInnerSpace("space0", spaceManager, innerSpace2);

            Console.WriteLine("Checking inner space attributes at space with key 'space0' to match innerSpace...");
            int CheckInnerSpace = testCheckInnerSpace("space0", spaceManager, innerSpace);

            Console.WriteLine("Checking inner space attributes at space with key 'space0' to match innerSpace2...");
            int CheckInnerSpace2 = testCheckInnerSpace("space0", spaceManager, innerSpace2);

            Console.WriteLine("Checking inner space attributes at space with key 'space0' to match a non-matching space...");
            int CheckInnerSpace3 = testCheckInnerSpace("space0", spaceManager, falseInnerSpace);

            Console.WriteLine("Testing RemoveSpace, removing all spaces...");
            int RemoveSpace = testRemoveSpace(spaceManager);

            Console.WriteLine("Checking spaces' attributes (Names, Notes, Dimensions) should no longer exist...");
            int CheckSpace2 = testCheckSpace(spaceManager, maxValue);

            Console.WriteLine("Testing AddSpace, creating " + maxValue + "spaces...");
            int AddSpace2 = testAddSpace(spaceManager, maxValue);

            Console.WriteLine("Testing UpdateSpace, updating space with key 'space0' with space 'spaceToUpdate'...");
            int UpdateSpace = testUpdateSpace("space0", spaceManager, spaceToUpdate);

            Console.WriteLine("Testing SearchSpace, searching for spaces with the expected name 'space' in it...");
            int SearchSpace = testSearchSpace("space", spaceManager);

            Console.WriteLine("Testing SearchSpace, searching for spaces with the expected name '0' in it...");
            int SearchSpace2 = testSearchSpace("0", spaceManager);

            Console.WriteLine("Testing SearchSpace, searching for spaces with the non-expected name 'failure' in it...");
            int SearchSpace3 = testSearchSpace("failure", spaceManager);

            /* Checking test success or failures */
            Console.WriteLine("\n---TEST RESULTS---");
            if (AddSpace != 1)
            {
                Console.WriteLine("AddSpace Test 1: Failed!");
            }
            else if (CheckSpace != 1)
            {
                Console.WriteLine("Check Space Attributes Test 1: Failed!");
            }
            else if (AddInnerSpace != 1)
            {
                Console.WriteLine("AddInnerSpace Test 1: Failed!");
            }
            else if (AddInnerSpace2 != 1)
            {
                Console.WriteLine("AddInnerSpace Test 2: Failed!");
            }
            else if (CheckInnerSpace != 1)
            {
                Console.WriteLine("Check Inner Space Test 1: Failed!");
            }
            else if (CheckInnerSpace2 != 1)
            {
                Console.WriteLine("Check Inner Space Test 2: Failed!");
            }
            else if (CheckInnerSpace3 != -1)
            { 
                Console.WriteLine("Check Inner Space Test 3: Failed!");
            }
            else if (RemoveSpace != 1)
            {
                Console.WriteLine("RemoveSpace Test 1: Failed!");
            }
            else if (CheckSpace2 != -4)
            {
                Console.WriteLine("Check Space Test 2: Failed!");
            }
            else if (AddSpace2 != 1)
            {
                Console.WriteLine("AddSpace Test 2: Failed!");
            }
            else if (UpdateSpace != 1)
            {
                Console.WriteLine("UpdateSpace Test 1: Failed!");
            }
            else if (SearchSpace != 1)
            {
                Console.WriteLine("SearchSpace Test 1: Failed!");
            }
            else if (SearchSpace2 != 1)
            {
                Console.WriteLine("SearchSpace Test 2: Failed!");
            }
            else if (SearchSpace3 != -1)
            {
                Console.WriteLine("SearchSpace Test 3: Failed!");
            }
            else
            {
                Console.WriteLine("All tests passed successfully!");
            }
            Console.ReadLine();
        }
    }
}
